package com.invengo.testrfid.app.utils;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.util.SparseIntArray;

import com.invengo.testrfid.R;

public class SoundPlayUtils {

    public static int SOUNT_SUCCESS = 1;
    public static int SOUNT_FAILED = 2;
    private static SoundPlayUtils soundPlayUtils;
    private Context context;
    private SoundPool soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
    private SparseIntArray soundPoolMap = new SparseIntArray();

    public SoundPlayUtils() {
    }

    public static SoundPlayUtils getInstance() {

        if (soundPlayUtils == null) {
            soundPlayUtils = new SoundPlayUtils();
        }
        return soundPlayUtils;
    }

    public void init(Context context) {

        this.context = context;
        soundPoolMap.put(1, soundPool.load(context, R.raw.tag, 1));
        soundPoolMap.put(2, soundPool.load(context, R.raw.notag, 2));
    }

    /**
     * 声音
     *
     * @param sound
     * @param loop
     */
    public void playSound(int sound, int loop) {

        AudioManager mgr = (AudioManager) context
                .getSystemService(Context.AUDIO_SERVICE);
        float streamVolumeCurrent = mgr
                .getStreamVolume(AudioManager.STREAM_MUSIC);
        float streamVolumeMax = mgr
                .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        float volume = streamVolumeCurrent / streamVolumeMax;
        soundPool.play(soundPoolMap.get(sound), volume, volume, 1, loop, 1f);
        // 参数：1、Map中取值 2、当前音量 3、最大音量 4、优先级 5、重播次数 6、播放速度
    }
}
